package com.me.myFramework.aop22.aop125;

import com.me.myFramework.aop22.IBuy;
import com.me.myFramework.aop22.Switch;
import com.me.myFramework.aop22.aop124.ProxyFactory124;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

import java.lang.reflect.Method;

/**
 * 《AOP-125》：  <br><br>
 *
 * TODO 以下变更点：
 *      使用 {@link ProxyFactory Spring ProxyFactory} 替换自定义的 {@link ProxyFactory124 ProxyFactory}
 *
 * @author ME
 */
public class MyAop125 {

    /**
     * 通过 模板模式 提取增强逻辑：方法执行前、正常返回、异常返回
     */
    public static void main(String[] args) throws Throwable {
        ProxyFactory proxyFactory = new ProxyFactory(new Switch());
        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP1] 就想看看谁买东西了：" + args[0]);
            }
        });
        proxyFactory.addAdvice(new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP2] 记录购买价格 " + returnValue);
            }
        });
        IBuy proxy = (IBuy) proxyFactory.getProxy();
        proxy.buy("Tom");
    }

}
